/*
 * @Author: Jingtian Zhang
 * @Date: 2021-06-06 16:45:46
 * @LastEditTime: 2021-06-06 19:23:05
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /illuvcoffee/src/coffee/coffee.service.ts
 */
import { Injectable, NotFoundException } from '@nestjs/common';
import { Coffee } from './entities/coffee.entity';

@Injectable()
export class CoffeeService {
  private coffees: Coffee[] = [
    {
      id: 1,
      name: 'Shipwreck Roast',
      brand: 'Buddy Brew',
      flavors: ['chocolate', 'vanilla'],
    },
  ];

  findOne(id: string) {
    const coffee =  this.coffees.find((item) => item.id === +id);
    if (!coffee){
      throw new NotFoundException(`Coffee #${id} not found`)
    }
    return coffee
  }

  findAll() {
    return this.coffees;
  }

  create(createCoffeeDto: any) {
    this.coffees.push(createCoffeeDto);
  }

  update(id: string, updateCoffeeDto: any) {
    const existingCoffee = this.findOne(id);
    if (existingCoffee) {
      // update the existing entity
    }
  }

  remove(id: string) {
    const coffeeIndex = this.coffees.findIndex((item) => item.id === +id);
    if (coffeeIndex >= 0) {
      this.coffees.splice(coffeeIndex, 1);
    }
  }
}
